package edu.vt.tcga.expression.level1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.vt.tcga.HibernateUtil;

public class GeneExpressionTable {

	private ExpressionParams parameters;
	private ExpressionStats statistics;
	private List<ExpressionFeatures> values;

	// Accessor methods
	public List<ExpressionFeatures> getValues() {
		return values;
	}

	public void setValues(List<ExpressionFeatures> values) {
		this.values = values;
	}

	public ExpressionParams getParameters() {
		return parameters;
	}

	public void setParameters(ExpressionParams parameters) {
		this.parameters = parameters;
	}

	public ExpressionStats getStatistics() {
		return statistics;
	}

	public void setStatistics(ExpressionStats statistics) {
		this.statistics = statistics;
	}

	// Constructors
	public GeneExpressionTable() {
		parameters = new ExpressionParams();
		statistics = new ExpressionStats();
		values = new ArrayList<ExpressionFeatures>();
	}

	public GeneExpressionTable(String filename) {
		this();
		this.loadFromFile(filename);
	}

	// Utility functions
	public void loadFromFile(String filename) {

		// Get current time
		long start = System.currentTimeMillis();

		System.out
				.println("Attempting to load expression genes analysis file: "
						+ filename);

		try {
			BufferedReader in = new BufferedReader(new FileReader(filename));
			String line = "";
			String delims = "\t";

			// Load Params
			line = in.readLine();
			// String[] types = line.split(delims);
			line = in.readLine();
			// String[] variables = line.split(delims);

			line = in.readLine();
			String[] tokens = line.split(delims);

//			System.out.println(line);
			
//			while ((line = in.readLine()) != null) {
				parameters.setProtocol_Name(tokens[1]);
				parameters.setProtocol_date(tokens[2]);
				parameters.setScan_Date(tokens[3]);
				parameters.setScan_ScannerName(tokens[4]);
				parameters.setScan_NumChannels(Integer.parseInt(tokens[5]));
				parameters.setScan_MicronsPerPixelX(Double
						.parseDouble(tokens[6]));
				parameters.setScan_MicronsPerPixelY(Double
						.parseDouble(tokens[7]));
				parameters.setScan_OriginalGUID(tokens[8]);
				parameters.setGrid_Name(tokens[9]);
				parameters.setGrid_Date(tokens[10]);

				parameters.setGrid_NumSubGridRows(Integer.parseInt(tokens[11]));
				parameters.setGrid_NumSubGridCols(Integer.parseInt(tokens[12]));
				parameters.setGrid_NumRows(Integer.parseInt(tokens[13]));
				parameters.setGrid_NumCols(Integer.parseInt(tokens[14]));
				parameters.setGrid_RowSpacing(Double.parseDouble(tokens[15]));
				parameters.setGrid_ColSpacing(Double.parseDouble(tokens[16]));
				parameters.setGrid_OffsetX(Double.parseDouble(tokens[17]));
				parameters.setGrid_OffsetY(Double.parseDouble(tokens[18]));
				parameters.setGrid_NomSpotWidth(Double.parseDouble(tokens[19]));
				parameters
						.setGrid_NomSpotHeight(Double.parseDouble(tokens[20]));

				parameters.setGrid_GenomicBuild(tokens[21]);
				parameters.setFeatureExtractor_Barcode(tokens[22]);
				parameters.setFeatureExtractor_Sample(tokens[23]);
				parameters.setFeatureExtractor_ScanFileName(tokens[24]);
				parameters.setFeatureExtractor_ArrayName(tokens[25]);
				parameters.setFeatureExtractor_DesignFileName(tokens[26]);
				parameters.setFeatureExtractor_PrintingFileName(tokens[27]);
				parameters.setFeatureExtractor_PatternName(tokens[28]);
				parameters.setFeatureExtractor_ExtractionTime(tokens[29]);
				parameters.setFeatureExtractor_UserName(tokens[30]);

				parameters.setFeatureExtractor_ComputerName(tokens[31]);
				parameters.setFeatureExtractor_ScanFileGUID(tokens[32]);
				parameters.setFeatureExtractor_IsXDRExtraction(Integer
						.parseInt(tokens[33]));
				parameters.setScan_NumScanPass(Integer.parseInt(tokens[34]));
				parameters.setGridPlacement_Version(tokens[35]);
				parameters.setGridPlacement_ArrayFormat(Integer
						.parseInt(tokens[36]));
				parameters.setGridPlacement_placementMode(Integer
						.parseInt(tokens[37]));
				parameters.setGridPlacement_ptsPerSide(Integer
						.parseInt(tokens[38]));
				parameters.setGridPlacement_minPeakSpacing(Double
						.parseDouble(tokens[39]));
				parameters.setGridPlacement_blocksizeAllowance(Double
						.parseDouble(tokens[40]));

				parameters.setGridPlacement_initialFilterWidth(Double
						.parseDouble(tokens[41]));
				parameters.setGridPlacement_coarseFilterWidth(Double
						.parseDouble(tokens[42]));
				parameters.setGridPlacement_fineFilterWidth(Double
						.parseDouble(tokens[43]));
				parameters.setGridPlacement_areaCutoffFactor(Double
						.parseDouble(tokens[44]));
				parameters.setGridPlacement_areaQuestionableFactor(Double
						.parseDouble(tokens[45]));
				parameters.setGridPlacement_areaSafetyFactor(Double
						.parseDouble(tokens[46]));
				parameters.setGridPlacement_lineHalfWidth(Integer
						.parseInt(tokens[47]));
				parameters.setGridPlacement_halfDepth(Integer
						.parseInt(tokens[48]));
				parameters.setGridPlacement_maximumSlope(Double
						.parseDouble(tokens[49]));
				parameters.setGridPlacement_gridWithChannel(Integer
						.parseInt(tokens[50]));

				parameters.setGridPlacement_relativeStartProjectionRow(Double
						.parseDouble(tokens[51]));
				parameters.setGridPlacement_relativeSizeProjectionRow(Double
						.parseDouble(tokens[52]));
				parameters.setGridPlacement_relativeStartProjectionCol(Double
						.parseDouble(tokens[53]));
				parameters.setGridPlacement_relativeSizeProjectionCol(Double
						.parseDouble(tokens[54]));
				parameters.setIterativeSpotFind_CornerAdjust(Integer
						.parseInt(tokens[55]));
				parameters.setIterativeSpotFind_AdjustThreshold(Double
						.parseDouble(tokens[56]));
				parameters.setIterativeSpotFind_MaxIterations(Integer
						.parseInt(tokens[57]));
				parameters.setIterativeSpotFind_FoundSpotThreshold(Double
						.parseDouble(tokens[58]));
				parameters.setIterativeSpotFind_NumCornerFeatures(Integer
						.parseInt(tokens[59]));
				parameters.setSpotAnalysis_Version(tokens[60]);

				parameters.setSpotAnalysis_weakthresh(Integer
						.parseInt(tokens[61]));
				parameters.setSpotAnalysis_MinimumNumPixels(Integer
						.parseInt(tokens[62]));
				parameters.setSpotAnalysis_RegionOfInterestMultiplier(Double
						.parseDouble(tokens[63]));
				parameters.setSpotAnalysis_convergence_factor(Double
						.parseDouble(tokens[64]));
				parameters.setSpotAnalysis_max_em_iter(Integer
						.parseInt(tokens[65]));
				parameters.setSpotAnalysis_max_reject_ratio(Double
						.parseDouble(tokens[66]));
				parameters.setSpotAnalysis_kmeans_rad_reject_factor(Double
						.parseDouble(tokens[67]));
				parameters.setSpotAnalysis_kmeans_cen_reject_factor(Double
						.parseDouble(tokens[68]));
				parameters.setSpotAnalysis_kmeans_moi_reject_factor(Double
						.parseDouble(tokens[69]));
				parameters.setSpotAnalysis_isspot_factor(Double
						.parseDouble(tokens[70]));

				parameters.setSpotAnalysis_isweakspot_factor(Double
						.parseDouble(tokens[71]));
				parameters.setSpotAnalysis_BackgroundThreshold(Double
						.parseDouble(tokens[72]));
				parameters
						.setSpotAnalysis_ROIType(Integer.parseInt(tokens[73]));
				parameters.setSpotAnalysis_UseNominalDiameterFromGT(Integer
						.parseInt(tokens[74]));
				parameters.setSpotAnalysis_RejectMethod(Integer
						.parseInt(tokens[75]));
				parameters.setSpotAnalysis_StatBoundFeat(Double
						.parseDouble(tokens[76]));
				parameters.setSpotAnalysis_StatBoundBG(Double
						.parseDouble(tokens[77]));
				parameters.setSpotAnalysis_SpotStatsMethod(Integer
						.parseInt(tokens[78]));
				parameters.setSpotAnalysis_CookiePercentage(Double
						.parseDouble(tokens[79]));
				parameters.setSpotAnalysis_ExclusionZonePercentage(Double
						.parseDouble(tokens[80]));

				parameters.setSpotAnalysis_EstimateLocalRadius(Integer
						.parseInt(tokens[81]));
				parameters.setSpotAnalysis_LocalBGRadius(Double
						.parseDouble(tokens[82]));
				parameters.setSpotAnalysis_SignalMethod(Integer
						.parseInt(tokens[83]));
				parameters.setSpotAnalysis_ComputePixelSkew(Integer
						.parseInt(tokens[84]));
				parameters.setSpotAnalysis_PixelSkewCookiePct(Double
						.parseDouble(tokens[85]));
				parameters.setSpotAnalysis_CentroidDiff(Integer
						.parseInt(tokens[86]));
				parameters.setSpotAnalysis_NozzleAdjust(Integer
						.parseInt(tokens[87]));
				parameters.setOutlierFlagger_Version(tokens[88]);
				parameters.setOutlierFlagger_NonUnifOLOn(Integer
						.parseInt(tokens[89]));
				parameters.setOutlierFlagger_FeatATerm(Double
						.parseDouble(tokens[90]));

				parameters.setOutlierFlagger_BGATerm(Double
						.parseDouble(tokens[91]));
				parameters.setOutlierFlagger_OLAutoComputeABC(Integer
						.parseInt(tokens[92]));
				parameters.setOutlierFlagger_FeatBCoeff(Double
						.parseDouble(tokens[93]));
				parameters.setOutlierFlagger_BGBCoeff(Double
						.parseDouble(tokens[94]));
				parameters.setOutlierFlagger_FeatCCoeff(Double
						.parseDouble(tokens[95]));
				parameters.setOutlierFlagger_BGCCoeff(Double
						.parseDouble(tokens[96]));
				parameters.setOutlierFlagger_FeatBCoeff2(Double
						.parseDouble(tokens[97]));
				parameters.setOutlierFlagger_BGBCoeff2(Double
						.parseDouble(tokens[98]));
				parameters.setOutlierFlagger_FeatCCoeff2(Double
						.parseDouble(tokens[99]));
				parameters.setOutlierFlagger_BGCCoeff2(Double
						.parseDouble(tokens[100]));

				parameters.setOutlierFlagger_PopnOLOn(Integer
						.parseInt(tokens[101]));
				parameters.setOutlierFlagger_MinPopulation(Integer
						.parseInt(tokens[102]));
				parameters.setOutlierFlagger_IQRatio(Double
						.parseDouble(tokens[103]));
				parameters.setOutlierFlagger_BackgroundIQRatio(Double
						.parseDouble(tokens[104]));
				parameters.setOutlierFlagger_UseQtest(Integer
						.parseInt(tokens[105]));
				parameters.setBGSubtractor_MultiplicativeDetrendOn(Integer
						.parseInt(tokens[106]));
				parameters.setBGSubtractor_MultDetrendWinFilter(Integer
						.parseInt(tokens[107]));
				parameters.setBGSubtractor_MultDetrendIncrement(Integer
						.parseInt(tokens[108]));
				parameters.setBGSubtractor_MultDetrendWindow(Integer
						.parseInt(tokens[109]));
				parameters.setBGSubtractor_MultDetrendNeighborhoodSize(Double
						.parseDouble(tokens[110]));

				parameters.setBGSubtractor_MultHighPassFilter(Integer
						.parseInt(tokens[111]));
				parameters
						.setBGSubtractor_PolynomialMultiplicativeDetrend(Integer
								.parseInt(tokens[112]));
				parameters
						.setBGSubtractor_NegCtrlThresholdMultDetrendFactor(Double
								.parseDouble(tokens[113]));
				parameters
						.setBGSubtractor_PolynomialMultiplicativeDetrendDegree(Integer
								.parseInt(tokens[114]));
				parameters.setBGSubtractor_TestMultDetrendOnCVs(Integer
						.parseInt(tokens[115]));
				parameters.setBGSubtractor_MultDetrendOnReplicates(Integer
						.parseInt(tokens[116]));
				parameters.setBGSubtractor_BGSubMethod(Integer
						.parseInt(tokens[117]));
				parameters.setBGSubtractor_MaxPVal(Double
						.parseDouble(tokens[118]));
				parameters.setBGSubtractor_WellAboveMulti(Double
						.parseDouble(tokens[119]));
				parameters.setBGSubtractor_BackgroundCorrectionOn(Integer
						.parseInt(tokens[120]));

				parameters.setBGSubtractor_CalculateSurfaceMetricsOn(Integer
						.parseInt(tokens[121]));
				parameters.setBGSubtractor_SpatialDetrendOn(Integer
						.parseInt(tokens[122]));
				parameters.setBGSubtractor_DetrendLowPassFilter(Integer
						.parseInt(tokens[123]));
				parameters.setBGSubtractor_DetrendLowPassPercentage(Integer
						.parseInt(tokens[124]));
				parameters.setBGSubtractor_DetrendLowPassWindow(Integer
						.parseInt(tokens[125]));
				parameters.setBGSubtractor_DetrendLowPassIncrement(Integer
						.parseInt(tokens[126]));
				parameters.setBGSubtractor_NegCtrlSpreadCoeff(Double
						.parseDouble(tokens[127]));
				parameters.setBGSubtractor_NegCtrlSpreadRobustOn(Double
						.parseDouble(tokens[128]));
				parameters.setBGSubtractor_AdditiveDetrendFeatureSet(Integer
						.parseInt(tokens[129]));
				parameters.setBGSubtractor_DetrendNeighborhoodSize(Double
						.parseDouble(tokens[130]));

				parameters.setBGSubtractor_ErrModelSignificance(Integer
						.parseInt(tokens[131]));
				parameters.setBGSubtractor_RobustNCStats(Integer
						.parseInt(tokens[132]));
				parameters.setBGSubtractor_RobustNCOutlierFactor(Double
						.parseDouble(tokens[133]));
				parameters.setBGSubtractor_ErrorModel(Integer
						.parseInt(tokens[134]));
				parameters.setBGSubtractor_MultErrorGreen(Double
						.parseDouble(tokens[135]));
				parameters.setBGSubtractor_MultErrorRed(Double
						.parseDouble(tokens[136]));
				parameters.setBGSubtractor_AutoEstimateAddErrorGreen(Integer
						.parseInt(tokens[137]));
				parameters.setBGSubtractor_AutoEstimateAddErrorRed(Integer
						.parseInt(tokens[138]));
				parameters.setBGSubtractor_MultNcAutoEstimate(Double
						.parseDouble(tokens[139]));
				parameters.setBGSubtractor_MultRMSAutoEstimate(Double
						.parseDouble(tokens[140]));

				parameters.setBGSubtractor_MultResidualsRMSAutoEstimate(Double
						.parseDouble(tokens[141]));
				parameters.setBGSubtractor_UseSurrogates(Integer
						.parseInt(tokens[142]));
				parameters.setBGSubtractor_Version(tokens[143]);
				parameters.setDyeNorm_Version(tokens[144]);
				parameters.setDyeNorm_SelectMethod(Integer
						.parseInt(tokens[145]));
				parameters.setDyeNorm_ArePosNegCtrlsOK(Integer
						.parseInt(tokens[146]));
				parameters.setDyeNorm_SignalCharacteristics(Integer
						.parseInt(tokens[147]));
				parameters.setDyeNorm_CorrMethod(Integer.parseInt(tokens[148]));
				parameters.setDyeNorm_LOWESSSmoothFactor(Double
						.parseDouble(tokens[149]));
				parameters.setDyeNorm_LOWESSNumSteps(Integer
						.parseInt(tokens[150]));

				parameters.setDyeNorm_RankTolerance(Double
						.parseDouble(tokens[151]));
				parameters.setDyeNorm_VariableRankTolerance(Integer
						.parseInt(tokens[152]));
				parameters.setDyeNorm_MaxRankedSize(Integer
						.parseInt(tokens[153]));
				parameters.setDyeNorm_IsBGPopnOLOn(Integer
						.parseInt(tokens[154]));
				parameters.setRatio_Version(tokens[155]);
				parameters.setRatio_PegLogRatioValue(Double
						.parseDouble(tokens[156]));
				parameters.setQCMetrics_UseSpikeIns(Integer
						.parseInt(tokens[157]));
				parameters.setQCMetrics_minReplicatePopulation(Integer
						.parseInt(tokens[158]));
				parameters.setQCMetrics_differentialExpressionPValue(Double
						.parseDouble(tokens[159]));
				parameters.setQCMetrics_MaxEdgeDefectThreshold(Double
						.parseDouble(tokens[160]));

				parameters.setQCMetrics_MaxEdgeNotFoundThreshold(Double
						.parseDouble(tokens[161]));
				parameters.setQCMetrics_MaxLocalBGNonUnifThreshold(Double
						.parseDouble(tokens[162]));
				parameters.setQCMetrics_MinNegCtrlSDev(Double
						.parseDouble(tokens[163]));
				parameters.setQCMetrics_MinReproducibility(Double
						.parseDouble(tokens[164]));
				parameters.setQCMetrics_Formulation(Integer
						.parseInt(tokens[165]));
				parameters.setQCMetrics_EnableDyeFlip(Integer
						.parseInt(tokens[166]));
				parameters.setQCMetrics_PercentileValueForSignal(Double
						.parseDouble(tokens[167]));
				parameters.setFeatureExtractor_Version(tokens[168]);
				parameters.setFeatureExtractor_SingleTextFileOutput(Integer
						.parseInt(tokens[169]));
				parameters.setFeatureExtractor_JPEGDownSampleFactor(Integer
						.parseInt(tokens[170]));

				parameters.setFeatureExtractor_ColorMode(Integer
						.parseInt(tokens[171]));
				parameters.setFeatureExtractor_QCReportType(Integer
						.parseInt(tokens[172]));
				parameters.setFeatureExtractor_OutputQCReportGraphText(Integer
						.parseInt(tokens[173]));

				// *
				line = in.readLine();

				// Load statistics
				// TYPE
				line = in.readLine();
				// types = line.split(delims);
				// STATS
				line = in.readLine();
				// variables = line.split(delims);

				// DATA
				line = in.readLine();
				tokens = line.split(delims);

//				System.out.println(line);

				statistics.setgDarkOffsetAverage(Double.parseDouble(tokens[1]));
				statistics.setgDarkOffsetMedian(Double.parseDouble(tokens[2]));
				statistics.setgDarkOffsetStdDev(Double.parseDouble(tokens[3]));
				statistics.setgDarkOffsetNumPts(Integer.parseInt(tokens[4]));
				statistics.setgSaturationValue(Integer.parseInt(tokens[5]));
				statistics.setrDarkOffsetAverage(Double.parseDouble(tokens[6]));
				statistics.setrDarkOffsetMedian(Double.parseDouble(tokens[7]));
				statistics.setrDarkOffsetStdDev(Double.parseDouble(tokens[8]));
				statistics.setrDarkOffsetNumPts(Integer.parseInt(tokens[9]));
				statistics.setrSaturationValue(Integer.parseInt(tokens[10]));

				statistics.setgAvgSig2BkgeQC(Double.parseDouble(tokens[11]));
				statistics.setrAvgSig2BkgeQC(Double.parseDouble(tokens[12]));
				statistics
						.setgAvgSig2BkgNegCtrl(Double.parseDouble(tokens[13]));
				statistics
						.setrAvgSig2BkgNegCtrl(Double.parseDouble(tokens[14]));
				statistics.setgRatioSig2BkgeQC_NegCtrl(Double
						.parseDouble(tokens[15]));
				statistics.setrRatioSig2BkgeQC_NegCtrl(Double
						.parseDouble(tokens[16]));
				statistics.setgNumSatFeat(Integer.parseInt(tokens[17]));
				statistics.setgLocalBGInlierNetAve(Double
						.parseDouble(tokens[18]));
				statistics.setgLocalBGInlierAve(Double.parseDouble(tokens[19]));
				statistics
						.setgLocalBGInlierSDev(Double.parseDouble(tokens[20]));

				statistics.setgLocalBGInlierNum(Integer.parseInt(tokens[21]));
				statistics
						.setgGlobalBGInlierAve(Double.parseDouble(tokens[22]));
				statistics.setgGlobalBGInlierSDev(Double
						.parseDouble(tokens[23]));
				statistics.setgGlobalBGInlierNum(Integer.parseInt(tokens[24]));
				statistics.setrNumSatFeat(Integer.parseInt(tokens[25]));
				statistics.setrLocalBGInlierNetAve(Double
						.parseDouble(tokens[26]));
				statistics.setrLocalBGInlierAve(Double.parseDouble(tokens[27]));
				statistics
						.setrLocalBGInlierSDev(Double.parseDouble(tokens[28]));
				statistics.setrLocalBGInlierNum(Integer.parseInt(tokens[29]));
				statistics
						.setrGlobalBGInlierAve(Double.parseDouble(tokens[30]));

				statistics.setrGlobalBGInlierSDev(Double
						.parseDouble(tokens[31]));
				statistics.setrGlobalBGInlierNum(Integer.parseInt(tokens[32]));
				statistics
						.setgNumFeatureNonUnifOL(Integer.parseInt(tokens[33]));
				statistics.setgNumPopnOL(Integer.parseInt(tokens[34]));
				statistics.setgNumNonUnifBGOL(Integer.parseInt(tokens[35]));
				statistics.setgNumPopnBGOL(Integer.parseInt(tokens[36]));
				statistics.setgOffsetUsed(Double.parseDouble(tokens[37]));
				statistics.setgGlobalFeatInlierAve(Double
						.parseDouble(tokens[38]));
				statistics.setgGlobalFeatInlierSDev(Double
						.parseDouble(tokens[39]));
				statistics
						.setgGlobalFeatInlierNum(Integer.parseInt(tokens[40]));

				statistics
						.setrNumFeatureNonUnifOL(Integer.parseInt(tokens[41]));
				statistics.setrNumPopnOL(Integer.parseInt(tokens[42]));
				statistics.setrNumNonUnifBGOL(Integer.parseInt(tokens[43]));
				statistics.setrNumPopnBGOL(Integer.parseInt(tokens[44]));
				statistics.setrOffsetUsed(Double.parseDouble(tokens[45]));
				statistics.setrGlobalFeatInlierAve(Double
						.parseDouble(tokens[46]));
				statistics.setrGlobalFeatInlierSDev(Double
						.parseDouble(tokens[47]));
				statistics
						.setrGlobalFeatInlierNum(Integer.parseInt(tokens[48]));
				statistics.setAllColorPrcntSat(Double.parseDouble(tokens[49]));
				statistics.setAnyColorPrcntSat(Double.parseDouble(tokens[50]));

				statistics.setAnyColorPrcntFeatNonUnifOL(Double
						.parseDouble(tokens[51]));
				statistics.setAnyColorPrcntBGNonUnifOL(Double
						.parseDouble(tokens[52]));
				statistics.setAnyColorPrcntFeatPopnOL(Double
						.parseDouble(tokens[53]));
				statistics.setAnyColorPrcntBGPopnOL(Double
						.parseDouble(tokens[54]));
				statistics.setTotalPrcntFeatOL(Double.parseDouble(tokens[55]));
				statistics.setgNumNegBGSubFeat(Integer.parseInt(tokens[56]));
				statistics.setgNonCtrlNumNegFeatBGSubSig(Integer
						.parseInt(tokens[57]));
				statistics.setgLinearDyeNormFactor(Double
						.parseDouble(tokens[58]));
				statistics.setgRMSLowessDNF(Double.parseDouble(tokens[59]));
				statistics.setrNumNegBGSubFeat(Integer.parseInt(tokens[60]));

				statistics.setrNonCtrlNumNegFeatBGSubSig(Integer
						.parseInt(tokens[61]));
				statistics.setrLinearDyeNormFactor(Double
						.parseDouble(tokens[62]));
				statistics.setrRMSLowessDNF(Double.parseDouble(tokens[63]));
				statistics.setDyeNormDimensionlessRMS(Double
						.parseDouble(tokens[64]));
				statistics.setDyeNormUnitWeightedRMS(Double
						.parseDouble(tokens[65]));
				statistics.setgSpatialDetrendRMSFit(Double
						.parseDouble(tokens[66]));
				statistics.setgSpatialDetrendRMSFilteredMinusFit(Double
						.parseDouble(tokens[67]));
				statistics.setgSpatialDetrendSurfaceArea(Double
						.parseDouble(tokens[68]));
				statistics.setgSpatialDetrendVolume(Double
						.parseDouble(tokens[69]));
				statistics.setgSpatialDetrendAveFit(Double
						.parseDouble(tokens[70]));

				statistics.setrSpatialDetrendRMSFit(Double
						.parseDouble(tokens[71]));
				statistics.setrSpatialDetrendRMSFilteredMinusFit(Double
						.parseDouble(tokens[72]));
				statistics.setrSpatialDetrendSurfaceArea(Double
						.parseDouble(tokens[73]));
				statistics.setrSpatialDetrendVolume(Double
						.parseDouble(tokens[74]));
				statistics.setrSpatialDetrendAveFit(Double
						.parseDouble(tokens[75]));
				statistics.setgNonCtrlNumSatFeat(Integer.parseInt(tokens[76]));
				statistics.setgNonCtrl99PrcntNetSig(Double
						.parseDouble(tokens[77]));
				statistics.setgNonCtrl50PrcntNetSig(Double
						.parseDouble(tokens[78]));
				statistics.setgNonCtrl1PrcntNetSig(Double
						.parseDouble(tokens[79]));
				statistics.setgNonCtrlMedPrcntCVBGSubSig(Double
						.parseDouble(tokens[80]));

				statistics.setrNonCtrlNumSatFeat(Integer.parseInt(tokens[81]));
				statistics.setrNonCtrl99PrcntNetSig(Double
						.parseDouble(tokens[82]));
				statistics.setrNonCtrl50PrcntNetSig(Double
						.parseDouble(tokens[83]));
				statistics.setrNonCtrl1PrcntNetSig(Double
						.parseDouble(tokens[84]));
				statistics.setrNonCtrlMedPrcntCVBGSubSig(Double
						.parseDouble(tokens[85]));
				statistics.setgCtrleQCNumSatFeat(Integer.parseInt(tokens[86]));
				statistics.setgCtrleQC99PrcntNetSig(Double
						.parseDouble(tokens[87]));
				statistics.setgCtrleQC50PrcntNetSig(Double
						.parseDouble(tokens[88]));
				statistics.setgCtrleQC1PrcntNetSig(Double
						.parseDouble(tokens[89]));
				statistics.setGeQCMedPrcntCVBGSubSig(Double
						.parseDouble(tokens[90]));

				statistics.setGeQCSig2BkgLow1(Double.parseDouble(tokens[91]));
				statistics.setGeQCSig2BkgLow2(Double.parseDouble(tokens[92]));
				statistics.setrCtrleQCNumSatFeat(Integer.parseInt(tokens[93]));
				statistics.setrCtrleQC99PrcntNetSig(Double
						.parseDouble(tokens[94]));
				statistics.setrCtrleQC50PrcntNetSig(Double
						.parseDouble(tokens[95]));
				statistics.setrCtrleQC1PrcntNetSig(Double
						.parseDouble(tokens[96]));
				statistics.setReQCMedPrcntCVBGSubSig(Double
						.parseDouble(tokens[97]));
				statistics.setReQCSig2BkgLow1(Double.parseDouble(tokens[98]));
				statistics.setReQCSig2BkgLow2(Double.parseDouble(tokens[99]));
				statistics.setgNegCtrlNumInliers(Integer.parseInt(tokens[100]));

				statistics
						.setgNegCtrlAveNetSig(Double.parseDouble(tokens[101]));
				statistics.setgNegCtrlSDevNetSig(Double
						.parseDouble(tokens[102]));
				statistics.setgNegCtrlAveBGSubSig(Double
						.parseDouble(tokens[103]));
				statistics.setgNegCtrlSDevBGSubSig(Double
						.parseDouble(tokens[104]));
				statistics.setrNegCtrlNumInliers(Integer.parseInt(tokens[105]));
				statistics
						.setrNegCtrlAveNetSig(Double.parseDouble(tokens[106]));
				statistics.setrNegCtrlSDevNetSig(Double
						.parseDouble(tokens[107]));
				statistics.setrNegCtrlAveBGSubSig(Double
						.parseDouble(tokens[108]));
				statistics.setrNegCtrlSDevBGSubSig(Double
						.parseDouble(tokens[109]));
				statistics.setgAveNumPixOLLo(Double.parseDouble(tokens[110]));

				statistics.setgAveNumPixOLHi(Double.parseDouble(tokens[111]));
				statistics.setgPixCVofHighSignalFeat(Double
						.parseDouble(tokens[112]));
				statistics.setgNumHighSignalFeat(Integer.parseInt(tokens[113]));
				statistics.setrAveNumPixOLLo(Double.parseDouble(tokens[114]));
				statistics.setrAveNumPixOLHi(Double.parseDouble(tokens[115]));
				statistics.setrPixCVofHighSignalFeat(Double
						.parseDouble(tokens[116]));
				statistics.setrNumHighSignalFeat(Integer.parseInt(tokens[117]));
				statistics.setNonCtrlAbsAveLogRatio(Double
						.parseDouble(tokens[118]));
				statistics.setNonCtrlSDevLogRatio(Double
						.parseDouble(tokens[119]));
				statistics.setNonCtrlSNRLogRatio(Double
						.parseDouble(tokens[120]));

				statistics
						.seteQCAbsAveLogRatio(Double.parseDouble(tokens[121]));
				statistics.seteQCSDevLogRatio(Double.parseDouble(tokens[122]));
				statistics.seteQCSNRLogRatio(Double.parseDouble(tokens[123]));
				statistics.setAddErrorEstimateGreen(Double
						.parseDouble(tokens[124]));
				statistics.setAddErrorEstimateRed(Double
						.parseDouble(tokens[125]));
				statistics.setTotalNumFeatures(Integer.parseInt(tokens[126]));
				statistics.setNonCtrlNumUpReg(Integer.parseInt(tokens[127]));
				statistics.setNonCtrlNumDownReg(Integer.parseInt(tokens[128]));
				statistics.seteQCObsVsExpLRSlope(Double
						.parseDouble(tokens[129]));
				statistics.seteQCObsVsExpLRIntercept(Double
						.parseDouble(tokens[130]));

				statistics.seteQCObsVsExpCorr(Double.parseDouble(tokens[131]));
				statistics.setNumIsNorm(Integer.parseInt(tokens[132]));
				statistics.setROIHeight(Double.parseDouble(tokens[133]));
				statistics.setROIWidth(Double.parseDouble(tokens[134]));
				statistics.setCentroidDiffX(Double.parseDouble(tokens[135]));
				statistics.setCentroidDiffY(Double.parseDouble(tokens[136]));
				statistics.setNumFoundFeat(Integer.parseInt(tokens[137]));
				statistics.setMaxNonUnifEdges(Double.parseDouble(tokens[138]));
				statistics.setMaxSpotNotFoundEdges(Double
						.parseDouble(tokens[139]));
				statistics.setgMultDetrendRMSFit(Double
						.parseDouble(tokens[140]));

				statistics.setrMultDetrendRMSFit(Double
						.parseDouble(tokens[141]));
				statistics.setgMultDetrendSurfaceAverage(Double
						.parseDouble(tokens[142]));
				statistics.setrMultDetrendSurfaceAverage(Double
						.parseDouble(tokens[143]));
				statistics.seteQCLowSigName1(tokens[144]);
				statistics.seteQCLowSigName2(tokens[145]);
				statistics.setgNonCtrl50PrcntBGSubSig(Double
						.parseDouble(tokens[146]));
				statistics.setrNonCtrl50PrcntBGSubSig(Double
						.parseDouble(tokens[147]));
				statistics.setgCtrleQC50PrcntBGSubSig(Double
						.parseDouble(tokens[148]));
				statistics.setrCtrleQC50PrcntBGSubSig(Double
						.parseDouble(tokens[149]));
				statistics.setgMedPrcntCVProcSignal(Double
						.parseDouble(tokens[150]));

				statistics.setrMedPrcntCVProcSignal(Double
						.parseDouble(tokens[151]));
				statistics.setGeQCMedPrcntCVProcSignal(Double
						.parseDouble(tokens[152]));
				statistics.setReQCMedPrcntCVProcSignal(Double
						.parseDouble(tokens[153]));
				statistics.setgOutlierFlagger_Auto_FeatB_Term(Double
						.parseDouble(tokens[154]));
				statistics.setrOutlierFlagger_Auto_FeatB_Term(Double
						.parseDouble(tokens[155]));
				statistics.setgOutlierFlagger_Auto_FeatC_Term(Double
						.parseDouble(tokens[156]));
				statistics.setrOutlierFlagger_Auto_FeatC_Term(Double
						.parseDouble(tokens[157]));
				statistics.setgOutlierFlagger_Auto_BgndB_Term(Double
						.parseDouble(tokens[158]));
				statistics.setrOutlierFlagger_Auto_BgndB_Term(Double
						.parseDouble(tokens[159]));
				statistics.setgOutlierFlagger_Auto_BgndC_Term(Double
						.parseDouble(tokens[160]));

				statistics.setrOutlierFlagger_Auto_BgndC_Term(Double
						.parseDouble(tokens[161]));
				statistics.setOutlierFlagger_FeatChiSq(Double
						.parseDouble(tokens[162]));
				statistics.setOutlierFlagger_BgndChiSq(Double
						.parseDouble(tokens[163]));
				statistics.setGriddingStatus(Integer.parseInt(tokens[164]));
				statistics.setNumGeneNonUnifOL(Integer.parseInt(tokens[165]));
				statistics.setTotalNumberOfReplicatedGenes(Integer
						.parseInt(tokens[166]));
				statistics.setgPercentileIntensityProcessedSignal(Double
						.parseDouble(tokens[167]));
				statistics.setrPercentileIntensityProcessedSignal(Double
						.parseDouble(tokens[168]));
				statistics.setExtractionStatus(Integer.parseInt(tokens[169]));
				statistics.setQCMetricResults(tokens[170]);

				statistics.setgNonCtrlNumWellAboveBG(Integer
						.parseInt(tokens[171]));
				statistics.setrNonCtrlNumWellAboveBG(Integer
						.parseInt(tokens[172]));
				statistics.setGridHasBeenOptimized(Boolean
						.parseBoolean(tokens[173]));
				statistics.setgNegCtrlSpread(Double.parseDouble(tokens[174]));
				statistics.setrNegCtrlSpread(Double.parseDouble(tokens[175]));
				statistics.setMetric_AnyColorPrcntFeatNonUnifOL(Double
						.parseDouble(tokens[176]));
				statistics
						.setMetric_AnyColorPrcntFeatNonUnifOL_IsInRange(Boolean
								.parseBoolean(tokens[177]));
				statistics.setMetric_absE1aObsVsExpCorr(Double
						.parseDouble(tokens[178]));
				statistics.setMetric_absE1aObsVsExpCorr_IsInRange(Boolean
						.parseBoolean(tokens[179]));
				statistics.setMetric_absE1aObsVsExpSlope(Double
						.parseDouble(tokens[180]));

				statistics.setMetric_absE1aObsVsExpSlope_IsInRange(Boolean
						.parseBoolean(tokens[181]));
				statistics.setMetric_gE1aMedCVBkSubSignal(Double
						.parseDouble(tokens[182]));
				statistics.setMetric_gE1aMedCVBkSubSignal_IsInRange(Boolean
						.parseBoolean(tokens[183]));
				statistics.setMetric_gNegCtrlAveBGSubSig(Double
						.parseDouble(tokens[184]));
				statistics.setMetric_gNegCtrlAveBGSubSig_IsInRange(Boolean
						.parseBoolean(tokens[185]));
				statistics.setMetric_gNegCtrlSDevBGSubSig(Double
						.parseDouble(tokens[186]));
				statistics.setMetric_gNegCtrlSDevBGSubSig_IsInRange(Boolean
						.parseBoolean(tokens[187]));
				statistics.setMetric_gNonCntrlMedCVBkSubSignal(Double
						.parseDouble(tokens[188]));
				statistics
						.setMetric_gNonCntrlMedCVBkSubSignal_IsInRange(Boolean
								.parseBoolean(tokens[189]));
				statistics.setMetric_rE1aMedCVBkSubSignal(Double
						.parseDouble(tokens[190]));

				statistics.setMetric_rE1aMedCVBkSubSignal_IsInRange(Boolean
						.parseBoolean(tokens[191]));
				statistics.setMetric_rNegCtrlAveBGSubSig(Double
						.parseDouble(tokens[192]));
				statistics.setMetric_rNegCtrlAveBGSubSig_IsInRange(Boolean
						.parseBoolean(tokens[193]));
				statistics.setMetric_rNegCtrlSDevBGSubSig(Double
						.parseDouble(tokens[194]));
				statistics.setMetric_rNegCtrlSDevBGSubSig_IsInRange(Boolean
						.parseBoolean(tokens[195]));
				statistics.setMetric_rNonCntrlMedCVBkSubSignal(Double
						.parseDouble(tokens[196]));
				statistics
						.setMetric_rNonCntrlMedCVBkSubSignal_IsInRange(Boolean
								.parseBoolean(tokens[197]));

				// *
				line = in.readLine();

				// Load Features
				// TYPE
				line = in.readLine();
				// FEATURES
				line = in.readLine();

				// DATA
				while ((line = in.readLine()) != null) {
					// Break out the fields
					tokens = line.split(delims);

					// Populate Object
					// NOTE: These should be ordered in the input string
					ExpressionFeatures e = new ExpressionFeatures();

					e.setFeatureNum(Integer.parseInt(tokens[1]));
					e.setRow(Integer.parseInt(tokens[2]));
					e.setCol(Integer.parseInt(tokens[3]));
					e.setChr_coord(tokens[4]);
					e.setSubTypeMask(Integer.parseInt(tokens[5]));
					e.setSubTypeName(tokens[6]);
					e.setStart(Integer.parseInt(tokens[7]));
					e.setSequence(tokens[8]);
					e.setProbeUID(Integer.parseInt(tokens[9]));
					e.setControlType(Integer.parseInt(tokens[10]));

					e.setProbeName(tokens[11]);
					e.setGeneName(tokens[12]);
					e.setSystematicName(tokens[13]);
					e.setDescription(tokens[14]);
					e.setPositionX(Double.parseDouble(tokens[15]));
					e.setPositionY(Double.parseDouble(tokens[16]));
					e.setLogRatio(Double.parseDouble(tokens[17]));
					e.setLogRatioError(Double.parseDouble(tokens[18]));
					e.setPValueLogRatio(Double.parseDouble(tokens[19]));
					e.setgSurrogateUsed(Double.parseDouble(tokens[20]));

					e.setrSurrogateUsed(Double.parseDouble(tokens[21]));
					e.setgIsFound(Boolean.parseBoolean(tokens[22]));
					e.setrIsFound(Boolean.parseBoolean(tokens[23]));
					e.setgProcessedSignal(Double.parseDouble(tokens[24]));
					e.setrProcessedSignal(Double.parseDouble(tokens[25]));
					e.setgProcessedSigError(Double.parseDouble(tokens[26]));
					e.setrProcessedSigError(Double.parseDouble(tokens[27]));
					e.setgNumPixOLHi(Integer.parseInt(tokens[28]));
					e.setrNumPixOLHi(Integer.parseInt(tokens[29]));
					e.setgNumPixOLLo(Integer.parseInt(tokens[30]));

					e.setrNumPixOLLo(Integer.parseInt(tokens[31]));
					e.setgNumPix(Integer.parseInt(tokens[32]));
					e.setrNumPix(Integer.parseInt(tokens[33]));
					e.setgMeanSignal(Double.parseDouble(tokens[34]));
					e.setrMeanSignal(Double.parseDouble(tokens[35]));
					e.setgMedianSignal(Double.parseDouble(tokens[36]));
					e.setrMedianSignal(Double.parseDouble(tokens[37]));
					e.setgPixSDev(Double.parseDouble(tokens[38]));
					e.setrPixSDev(Double.parseDouble(tokens[39]));
					e.setgPixNormIQR(Double.parseDouble(tokens[40]));

					e.setrPixNormIQR(Double.parseDouble(tokens[41]));
					e.setgBGNumPix(Integer.parseInt(tokens[42]));
					e.setrBGNumPix(Integer.parseInt(tokens[43]));
					e.setgBGMeanSignal(Double.parseDouble(tokens[44]));
					e.setrBGMeanSignal(Double.parseDouble(tokens[45]));
					e.setgBGMedianSignal(Double.parseDouble(tokens[46]));
					e.setrBGMedianSignal(Double.parseDouble(tokens[47]));
					e.setgBGPixSDev(Double.parseDouble(tokens[48]));
					e.setrBGPixSDev(Double.parseDouble(tokens[49]));
					e.setgBGPixNormIQR(Double.parseDouble(tokens[50]));

					e.setrBGPixNormIQR(Double.parseDouble(tokens[51]));
					e.setgNumSatPix(Integer.parseInt(tokens[52]));
					e.setrNumSatPix(Integer.parseInt(tokens[53]));
					e.setgIsSaturated(Boolean.parseBoolean(tokens[54]));
					e.setrIsSaturated(Boolean.parseBoolean(tokens[55]));
					e.setPixCorrelation(Double.parseDouble(tokens[56]));
					e.setBGPixCorrelation(Double.parseDouble(tokens[57]));
					e.setgIsFeatNonUnifOL(Boolean.parseBoolean(tokens[58]));
					e.setrIsFeatNonUnifOL(Boolean.parseBoolean(tokens[59]));
					e.setgIsBGNonUnifOL(Boolean.parseBoolean(tokens[60]));

					e.setrIsBGNonUnifOL(Boolean.parseBoolean(tokens[61]));
					e.setgIsFeatPopnOL(Boolean.parseBoolean(tokens[62]));
					e.setrIsFeatPopnOL(Boolean.parseBoolean(tokens[63]));
					e.setgIsBGPopnOL(Boolean.parseBoolean(tokens[64]));
					e.setrIsBGPopnOL(Boolean.parseBoolean(tokens[65]));
					e.setIsManualFlag(Boolean.parseBoolean(tokens[66]));
					e.setgBGSubSignal(Double.parseDouble(tokens[67]));
					e.setrBGSubSignal(Double.parseDouble(tokens[68]));
					e.setgBGSubSigError(Double.parseDouble(tokens[69]));
					e.setrBGSubSigError(Double.parseDouble(tokens[70]));

					e.setBGSubSigCorrelation(Double.parseDouble(tokens[71]));
					e.setgIsPosAndSignif(Boolean.parseBoolean(tokens[72]));
					e.setrIsPosAndSignif(Boolean.parseBoolean(tokens[73]));
					e.setgPValFeatEqBG(Double.parseDouble(tokens[74]));
					e.setrPValFeatEqBG(Double.parseDouble(tokens[75]));
					e.setgNumBGUsed(Integer.parseInt(tokens[76]));
					e.setrNumBGUsed(Integer.parseInt(tokens[77]));
					e.setgIsWellAboveBG(Boolean.parseBoolean(tokens[78]));
					e.setrIsWellAboveBG(Boolean.parseBoolean(tokens[79]));
					e.setgBGUsed(Double.parseDouble(tokens[80]));

					e.setrBGUsed(Double.parseDouble(tokens[81]));
					e.setgBGSDUsed(Double.parseDouble(tokens[82]));
					e.setrBGSDUsed(Double.parseDouble(tokens[83]));
					e.setIsNormalization(Boolean.parseBoolean(tokens[84]));
					e.setgDyeNormSignal(Double.parseDouble(tokens[85]));
					e.setrDyeNormSignal(Double.parseDouble(tokens[86]));
					e.setgDyeNormError(Double.parseDouble(tokens[87]));
					e.setrDyeNormError(Double.parseDouble(tokens[88]));
					e.setDyeNormCorrelation(Double.parseDouble(tokens[89]));
					e.setErrorModel(Integer.parseInt(tokens[90]));

					e.setxDev(Double.parseDouble(tokens[91]));
					e.setgSpatialDetrendIsInFilteredSet(Boolean
							.parseBoolean(tokens[92]));
					e.setrSpatialDetrendIsInFilteredSet(Boolean
							.parseBoolean(tokens[93]));
					e.setgSpatialDetrendSurfaceValue(Double
							.parseDouble(tokens[94]));
					e.setrSpatialDetrendSurfaceValue(Double
							.parseDouble(tokens[95]));
					e.setSpotExtentX(Double.parseDouble(tokens[96]));
					e.setSpotExtentY(Double.parseDouble(tokens[97]));
					e.setgNetSignal(Double.parseDouble(tokens[98]));
					e.setrNetSignal(Double.parseDouble(tokens[99]));
					e.setgMultDetrendSignal(Double.parseDouble(tokens[100]));

					e.setrMultDetrendSignal(Double.parseDouble(tokens[101]));
					e.setgProcessedBackground(Double.parseDouble(tokens[102]));
					e.setrProcessedBackground(Double.parseDouble(tokens[103]));
					e.setgProcessedBkngError(Double.parseDouble(tokens[104]));
					e.setrProcessedBkngError(Double.parseDouble(tokens[105]));
					e.setIsUsedBGAdjust(Boolean.parseBoolean(tokens[106]));
					e.setgInterpolatedNegCtrlSub(Double
							.parseDouble(tokens[107]));
					e.setrInterpolatedNegCtrlSub(Double
							.parseDouble(tokens[108]));
					e.setgIsInNegCtrlRange(Boolean.parseBoolean(tokens[109]));
					e.setrIsInNegCtrlRange(Boolean.parseBoolean(tokens[110]));

					e.setgIsUsedInMD(Boolean.parseBoolean(tokens[111]));
					e.setrIsUsedInMD(Boolean.parseBoolean(tokens[112]));

					values.add(e);
				}

//			}
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}

		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis() - start;
		double seconds = elapsedTimeMillis / 1000.0;

		System.out.println("Loaded expression genes analysis data in "
				+ seconds + " seconds");
	}

	// Save the class out to the database
	public void saveToDatabase() {

		// Get current time
		long start = System.currentTimeMillis();

		try {
			Session session = HibernateUtil.getSession();
	        Transaction tx = session.beginTransaction();
	        int i = 0;

	        session.save(this.getParameters());
			session.save(this.getStatistics());
	        
			for (ExpressionFeatures features : this.getValues()) {
				session.save(features);
		        
	            if ( i % 20 == 0 ) { //Same as the JDBC batch size
	                //flush a batch of inserts and release memory:
	                session.flush();
	                session.clear();
	            }
	            
		        i++;
	        }
	        
	        tx.commit();
	        session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Get elapsed time in milliseconds
		long elapsedTimeMillis = System.currentTimeMillis() - start;
		double seconds = elapsedTimeMillis / 1000.0;

		System.out.println("Saved expression genes data in " + seconds
				+ " seconds");
	}
}
